package site.luojie;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.I0Itec.zkclient.IZkDataListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 配置监听
 *
 * @author jie.luo
 * @since 2020-08-13
 */
public class DataSourceConfigWater implements IZkDataListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void handleDataChange(String path, Object data) throws Exception {

        logger.debug("监听节点数据发生改变...path:{} , data: {}", path, data);

        ObjectMapper objectMapper = new ObjectMapper();
        DataSourceInfo dataSourceInfo = objectMapper.readValue((String) data, DataSourceInfo.class);
        DruidDataSourceFactory.build(dataSourceInfo,true);

    }

    @Override
    public void handleDataDeleted(String path) throws Exception {
        logger.debug("监听节点被删除...path:{}", path);
    }
}
